﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Universe.TrafficProfiler
{
    public partial class ProcessListForm : Form
    {
        public ProcessListForm()
        {
            InitializeComponent();

            if (ApplySettings())
            {
                this.StartPosition = FormStartPosition.Manual;
            }
            else
            {
                this.StartPosition = FormStartPosition.CenterScreen;
            }

        }


        private bool IsApplingGrid = false;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            dataGridView1.CellContentClick += DataGridView1_OnCellContentClick;

            EventHandler eh = delegate { SaveSettings(); };
            this.Resize += eh;
            this.Move += eh;
            dataGridView1.ColumnWidthChanged += Grid_ColumnWidthChanged;

            Bind(ModelBuilder.Build());
        }

        private void DataGridView1_OnCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            if (dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "Id")
            {
                ProcessListModel.ProcessInfo pi = (ProcessListModel.ProcessInfo) dataGridView1.Rows[e.RowIndex].DataBoundItem;
                
                ProfilerForm pf = new ProfilerForm(pi);
                pf.Owner = this;
                pf.Show();

            }
        }

        public void Bind(ProcessListModel model)
        {

            BuildColumns();
            dataGridView1.DataSource = model.Processes;

        }

        void BuildColumns()
        {
            dataGridView1.Columns.Clear();

            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.AllowUserToDeleteRows = false;
            dataGridView1.AllowUserToOrderColumns = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.BackColor;
            dataGridView1.DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor;

            dataGridView1.AutoGenerateColumns = false;

            {
                var c1 = new DataGridViewCheckBoxColumn(false);
                c1.HeaderText = "";
                c1.Name = c1.DataPropertyName = "Selected";
                c1.Resizable = DataGridViewTriState.False;
                c1.Width = 25;
                c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                // dataGridView1.Columns.Add(c1);

            }

            {
                var c1 = new DataGridViewTextBoxColumn();
                c1.HeaderText = "PID";
                c1.ReadOnly = true;
                c1.Width = 80;
                c1.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                c1.Name = c1.DataPropertyName = "Id";
                // dataGridView1.Columns.Add(c1);
            }

            {
                var c1 = new DataGridViewLinkColumn();
                c1.HeaderText = "PID";
                c1.ReadOnly = true;
                c1.Width = 80;
                c1.CellTemplate.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                c1.Name = c1.DataPropertyName = "Id";
                c1.SortMode = DataGridViewColumnSortMode.Automatic;
                dataGridView1.Columns.Add(c1);
            }

            {
                var c1 = new DataGridViewTextBoxColumn();
                c1.HeaderText = "File";
                c1.ReadOnly = true;
                c1.Name = c1.DataPropertyName = "File";
                c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                c1.Width = 280;
                dataGridView1.Columns.Add(c1);
            }

            {
                var c1 = new DataGridViewTextBoxColumn();
                c1.HeaderText = "Path";
                c1.ReadOnly = true;
                c1.Name = c1.DataPropertyName = "Path";
                c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                c1.Width = 40;
                dataGridView1.Columns.Add(c1);
            }

            {
                var c1 = new DataGridViewTextBoxColumn();
                c1.HeaderText = "Accepted Requests";
                c1.ReadOnly = true;
                c1.Name = c1.DataPropertyName = "ServerRequests";
                c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                c1.Width = 110;
                dataGridView1.Columns.Add(c1);
            }

            {
                var c1 = new DataGridViewTextBoxColumn();
                c1.HeaderText = "Sent Requests";
                c1.ReadOnly = true;
                c1.Name = c1.DataPropertyName = "ClientRequests";
                c1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
                c1.Width = 110;
                dataGridView1.Columns.Add(c1);
            }

            ApplyGridSettings();
        }


        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            Bind(ModelBuilder.Build());
        }

    }
}
